package com.fw.ui.view.settings.passwordpolicy.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.ui.view.event.AbstractValueChangeListener;
import com.fw.ui.view.settings.passwordpolicy.EditPasswordPolicyView;
import com.vaadin.data.Property.ValueChangeEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditPasswordPolicyMinSpecialCharacterValueChangeListener extends AbstractValueChangeListener {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean validate(ValueChangeEvent event) throws Exception {
	EditPasswordPolicyView editPasswordPolicyView = (EditPasswordPolicyView) this.getBasedComponent();
	editPasswordPolicyView.getRow5Validate().setVisible(false);
	editPasswordPolicyView.getLblMinSpecialCharacterValidateMessage().setValue("");
	editPasswordPolicyView.getTxtMinSpecialCharacter().removeStyleName("v-textfield-error");
	boolean minPasswordLengthValid = editPasswordPolicyView.getTxtMinSpecialCharacter().isValid();
	if (!minPasswordLengthValid) {
	    editPasswordPolicyView.getTxtMinSpecialCharacter().addStyleName("v-textfield-error");
	    if (editPasswordPolicyView.getTxtMinSpecialCharacter().getErrorMessage() != null) {
		editPasswordPolicyView.getLblMinSpecialCharacterValidateMessage().setValue(
			editPasswordPolicyView.getTxtMinSpecialCharacter().getErrorMessage().getFormattedHtmlMessage());
	    }
	    editPasswordPolicyView.getRow5Validate().setVisible(true);
	}
	return true;
    }

    @Override
    public void change(ValueChangeEvent event) throws Exception {
    }

}
